package com.pphh.demo.dbshard;

import com.pphh.demo.dbshard.dao.OrderItemRepository;
import com.pphh.demo.dbshard.dao.OrderRepository;
import com.pphh.demo.dbshard.entity.Order;
import com.pphh.demo.dbshard.entity.OrderItem;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * Please add description here.
 *
 * @author huangyinhuang
 * @date 8/21/2018
 */
@Service
public class DemoService {

    private Logger logger = LoggerFactory.getLogger(DemoService.class);

    @Resource
    private OrderRepository orderRepository;

    @Resource
    private OrderItemRepository orderItemRepository;

    public void demo() {
        logger.info("test");
        List<Long> orderIds = new ArrayList<>(10);
        List<Long> orderItemIds = new ArrayList<>(10);
        System.out.println("1.Insert--------------");
        for (int i = 0; i < 10; i++) {
            for (int j = 0; j < 20; j++) {
                Order order = new Order();
                order.setUserId(i);
                order.setOrderId(j);
                order.setStatus("INSERT_TEST");
                long orderId = orderRepository.save(order).getOrderId();
                orderIds.add(orderId);
                OrderItem item = new OrderItem();
                item.setOrderId(j);
                item.setUserId(i);
                orderItemIds.add(orderItemRepository.save(item).getOrderItemId());
            }
        }
        List<OrderItem> orderItems = orderItemRepository.findAll();
        System.out.println(orderItemRepository.findAll());
        System.out.println("2.Delete--------------");
        if (orderItems.size() > 0) {
            for (Long each : orderItemIds) {
                orderItemRepository.delete(each);
            }
            for (Long each : orderIds) {
                orderRepository.delete(each);
            }
        }
        System.out.println(orderItemRepository.findAll());
    }

}
